﻿// -----------------------------------------------------------------------
// <copyright file="EnergySphere.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Particle3DEffect;
using ObjectLibrary;

namespace FighterLibrary
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class EnergySphere 
    {
        private ParticleSystem _sphere;
        private ParticleSystem _explosion;
        private bool _isExplosion;
        public const float EXPLOSE_RANGE = 10;

   
        public bool IsExplosion
        {
            get { return _isExplosion; }
            set { _isExplosion = value; }
        }

        private Vector3 _position;

        public Vector3 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        private Vector3 vectorVelocitySphere;
        //private double velocitySphere = 0.02f;
        private double timeSphereFly = 5000;
        private Vector3 targetSphere;
        private bool _isFlying = false;
        private double timeFlying = 0;
        private bool isDead = false;
        public static int DAMAGE_ENERGY_SPHERE = 5;
        public static int MANA_PER_THROW = 30;

        public bool IsDead
        {
            get { return isDead; }
            set { isDead = value; }
        }
   
        private PhysicalEngine physicalEngine = new PhysicalEngine();
        public void SetStart(Vector3 pos, Vector3 direct, float force)
        {
            _isFlying = true;

            _position = pos;

            direct.Normalize();
            direct *= force;
            physicalEngine.Reset();
            physicalEngine.setForce(direct);
            physicalEngine.ArchimedesForce = 80f;
        }
        private BoundingSphere _bounding;
        private float boundingRadius = 3;
        private bool _isCollision = false;
        private double timeExplosion = 1000;
        private double currentTimeExplosion = 0;

        public BoundingSphere Bounding
        {
            get { return _bounding; }
            set { _bounding = value; }
        }

        public bool IsCollision
        {
            get { return _isCollision; }
            set { _isCollision = value; }
        }


        //public void Set
        public bool IsFlying
        {
            get { return _isFlying; }
            set { _isFlying = value; }
        }

        public Vector3 VectorVelocitySphere
        {
            get { return vectorVelocitySphere; }
            set { vectorVelocitySphere = value; }
        }

        public double TimeSphereFly
        {
            get { return timeSphereFly; }
            set { timeSphereFly = value; }
        }

        public Vector3 TargetSphere
        {
            get { return targetSphere; }
            set { targetSphere = value; }
        }

        public ParticleSystem Sphere
        {
            get { return _sphere; }
            set { _sphere = value; }
        }

        public ParticleSystem Explosion
        {
            get { return _explosion; }
            set { _explosion = value; }
        }

        public EnergySphere(Game game)
        {
            ParticleSystem energySphereParticle = new EnergySphereParticle(game, game.Content);
            energySphereParticle.DrawOrder = 600;
            game.Components.Add(energySphereParticle);
            Sphere = energySphereParticle;

            ParticleSystem explosionParticle = new ExplosionParticleSystem(game, game.Content);
            explosionParticle.DrawOrder = 600;
            game.Components.Add(explosionParticle);
            Explosion = explosionParticle;
        }

        public void UpdateEnergySphere(GameTime gameTime)
        {
            if (IsFlying)
            {
                physicalEngine.update(gameTime.ElapsedGameTime.TotalSeconds, false);
                _position = physicalEngine.updatePosition(_position, gameTime.ElapsedGameTime.TotalSeconds);
                timeFlying += gameTime.ElapsedGameTime.Milliseconds;
                const int fireParticlesPerFrame = 20;
                //vectorVelocitySphere += (float)velocitySphere * (targetSphere - StartPosition);
                // Create a number of fire particles, randomly positioned around a circle.
                for (int i = 0; i < fireParticlesPerFrame; i++)
                {
                    Sphere.AddParticle(_position, Vector3.Zero);
                }
                _bounding = new BoundingSphere(_position, boundingRadius);
                if (timeFlying > timeSphereFly)
                {
                    reset();
                }
            }
            if (IsCollision)
            {
                currentTimeExplosion += gameTime.ElapsedGameTime.Milliseconds;
                const int fireParticlesPerFrame = 20;
                //vectorVelocitySphere += (float)velocitySphere * (targetSphere - StartPosition);
                // Create a number of fire particles, randomly positioned around a circle.
                for (int i = 0; i < fireParticlesPerFrame; i++)
                {
                    Explosion.AddParticle(_position, Vector3.Zero);
                }

                if (currentTimeExplosion > timeExplosion)
                {
                    reset();
                }
            }
        }

        private void reset()
        {
            _isFlying = false;
            vectorVelocitySphere = Vector3.Zero;
            timeFlying = 0;
            _bounding = new BoundingSphere(new Vector3(-1,-1,-1), 0);
            currentTimeExplosion = 0;
            IsCollision = false;
            isDead = true;
        }
    }
}
